package com.goldenautumn.prefabricate.activity.home

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.blankj.utilcode.util.LogUtils
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.activity.category.CategoryLayoutAdapter
import com.goldenautumn.prefabricate.activity.mall.GoodsDetailsActivity
import com.goldenautumn.prefabricate.business.entity.GoodsInfoVO
import com.goldenautumn.prefabricate.business.entity.SearchParams
import com.goldenautumn.prefabricate.business.mall.GoodsInfoUiState
import com.goldenautumn.prefabricate.business.mall.MallViewModel
import com.goldenautumn.prefabricate.business.order.OrderViewModel
import com.goldenautumn.prefabricate.databinding.FragmentTypeBinding
import com.goldenautumn.prefabricate.utils.Constants.BUSINESS_PAGE_DATA
import com.goldenautumn.prefabricate.utils.Constants.ORDER_TYPE_ALL
import kotlinx.coroutines.isActive
import kotlinx.coroutines.selects.select
import qncx.external.common.base.BaseFragment
import qncx.external.common.utils.intent

class TypeFragment : BaseFragment<FragmentTypeBinding>() {

    companion object {
        fun newInstance(type: String,isGird:Boolean): TypeFragment {
            val fragment = TypeFragment()
            val bundle = Bundle()
            bundle.putString(BUSINESS_PAGE_DATA, type)
            bundle.putBoolean("IS_GIRD", isGird)
            fragment.arguments = bundle
            return fragment
        }
    }

    private val mAdapter by lazy {
        CategoryLayoutAdapter()
    }

    private val mMallModel by viewModels<MallViewModel>()

    private val type: String?
        get() = arguments?.getString(BUSINESS_PAGE_DATA)

    private val isGird: Boolean?
        get() = arguments?.getBoolean("IS_GIRD",false)

    override fun getLayoutId(): Int  = R.layout.fragment_type

    override suspend fun initView() {
        mBinding.typeRv.layoutManager = if(isGird == true) StaggeredGridLayoutManager(2, RecyclerView.VERTICAL) else LinearLayoutManager(mContext)
        mBinding.typeRv.adapter = mAdapter

        mMallModel.getGoodsList(SearchParams(categoryID = type))

        mAdapter.setOnItemClickListener { adapter,_,position ->
            var item = adapter.getItem(position)
            startActivity(GoodsDetailsActivity::class.intent.putExtra(BUSINESS_PAGE_DATA,item?.goods_id))
        }

        if(isGird == false) {
            val layoutParams = mBinding.typeRv.layoutParams as ViewGroup.MarginLayoutParams
            layoutParams.setMargins(0,0,0,0)
            mBinding.typeRv.layoutParams = layoutParams
        }

        while (isActive) {
            select {
                mMallModel.goodsDetailsRequests.onReceive {
                    when(it) {
                        is GoodsInfoUiState.GoodsList -> {
                            var data = ArrayList<GoodsInfoVO>()
                            it.goodsList.forEach {
                                it.itemType = if(isGird == true) 2 else 1
                                data.add(it)
                            }
                            mAdapter.submitList(it.goodsList)
                        }
                        else -> {}
                    }
                }
            }
        }

    }

}